/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tj.eskhata.pos.web.report;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import tj.eskhata.pos.common.WiaSession;
import tj.eskhata.pos.domain.ClPart;
import tj.eskhata.pos.domain.ClPoint;
import tj.eskhata.pos.domain.Client;
import tj.eskhata.pos.domain.PsDoctrans;
import tj.eskhata.pos.domain.Sost;
import tj.eskhata.pos.domain.User;
import tj.eskhata.pos.services.PosService;


/**
 * implementation of IDataProvider for contacts that keeps track of sort information
 * 
 * @author igor
 * 
 */
public class PSDocDP extends SortableDataProvider<PsDoctrans>
{
	@SpringBean
	private PosService service;
	private User user;
	private Date begDate;
	private Date endDate;
	private String clName;
	private String docNumber;
	private Long point;
	private Long part;
	 

	public Long getPoint() {
		return point;
	}

	public void setPoint(Long point) {
		this.point = point;
	}

	public Long getPart() {
		return part;
	}

	public void setPart(Long part) {
		this.part = part;
	}
	
	public String getDocNumber() {
		return docNumber;
	}

	public void setDocNumber(String docNumber) {
		this.docNumber = docNumber;
	}

	public String getClName() {
		return clName;
	}

	public void setClName(String clName) {
		this.clName = clName;
	}

	/**
	 * constructor
	 */
	public PSDocDP()
	{
		 InjectorHolder.getInjector().inject(this);	
	}
	
	public PSDocDP( User user,Date begDate, Date endDate)
	{
		 InjectorHolder.getInjector().inject(this);
		 this.user=user;
		 this.begDate=begDate;
		 this.endDate=endDate;
	}
	
	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
	 */
	public Iterator<PsDoctrans> iterator(int first, int count)
	{
		/*
		//Sost sostP=null;
		//Sost sostS=null;
		//Criterion res =null;
		
		if(WiaSession.get().getIsAdmin())
			res=Restrictions.eq("clPart.id", WiaSession.get().getPoint().getClPart().getId());
		else
			res=Restrictions.eq("createUser.id", user.getId());		
		
		//sostP=service.getSost("PAYD");
		//sostS=service.getSost("SEND");*/
		Long userId= null;
		if(WiaSession.get().getIsAdmin())
			part = WiaSession.get().getPoint().getClPart().getId();
		else
			userId = user.getId();
				
		
		List<PsDoctrans> lst=service.getCommitedPsDoctrans(begDate, endDate, point, part, clName, docNumber, userId);
		/*List<PsDoctrans> lst=service.findByCriteriaPsDoctrans(
									res,
									Restrictions.or(Restrictions.eq("sost.id", sostP.getId()), Restrictions.eq("sost.id", sostS.getId())),
									Restrictions.le("datedoc", endDate),
									Restrictions.ge("datedoc", begDate)//,
									//Restrictions.like("client.name", clName)
									);*/
		int n=lst.size()>300?300:lst.size();
		return lst.subList(first, first+count).iterator();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size()
	{	
		/*Sost sostP=null;
		Sost sostS=null;
		Criterion res =null;
		
		if(WiaSession.get().getIsAdmin())
			res=Restrictions.eq("clPart.id", WiaSession.get().getPoint().getClPart().getId());
		else
			res=Restrictions.eq("createUser.id", user.getId());
		
		sostP=service.getSost("PAYD");
		sostS=service.getSost("SEND");
		int i=service.findByCriteriaPsDoctrans(
				res,
				Restrictions.or(Restrictions.eq("sost.id", sostP.getId()), Restrictions.eq("sost.id", sostS.getId())),
				Restrictions.le("datedoc", endDate),
				Restrictions.ge("datedoc", begDate)
				).size();*/
		Long userId= null;
		if(WiaSession.get().getIsAdmin())
			part = WiaSession.get().getPoint().getClPart().getId();
		else
			userId = user.getId();
		
		int i = service.getCommitedPsDoctrans(begDate, endDate, point, part, clName, docNumber, userId).size();
		
		return i<300?i:300;
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<PsDoctrans> model(final PsDoctrans object)
	{
		return new AbstractReadOnlyModel<PsDoctrans>() {
			@Override
			public PsDoctrans getObject() {
				return object;
			}
		};
		//return new DetachableContactModel(object);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Date getBegDate() {
		return begDate;
	}

	public void setBegDate(Date begDate) {
		this.begDate = begDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

}
